package com.ygqh.baby.controller.mobile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.service.FileService;

import sun.misc.BASE64Decoder;

@Controller
@RequestMapping(value = "/mobile/file")
public class FileOsController {

	@Value("${image.domain}")
	private String imageDomain;
	@Autowired
	private FileService fileService;

	@RequestMapping(value = "uploadFile")
	@ResponseBody
	public Map<Object, Object> uploadFile(@RequestParam("uploadFile") MultipartFile uploadFile, FileType fileType, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<Object, Object> result = new HashMap<Object, Object>();
		if (fileType == null) {
			fileType = FileType.Temp;
		}

		if (!uploadFile.isEmpty()) {
			if (fileType.getMaxSize() != null && fileType.getMaxSize() < uploadFile.getSize()) {
				result.put("message", "文件尺寸过大");
				return result;
			}
			String url = fileService.upload2Oss(fileType, uploadFile);
			result.put("fileUrl", url);
		}
		return result;
	}

	@RequestMapping(value = "uploadImage")
	@ResponseBody
	public Map<Object, Object> uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile, @RequestParam(required = true) FileType[] fileType,Boolean isFill, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(isFill == null)
			isFill = true;
		Map<Object, Object> result = new HashMap<Object, Object>();
		if (!uploadFile.isEmpty()) {
			String url = fileService.uploadImage(uploadFile, fileType, request,isFill);
			result.put("fileUrl", url);
		}
		return result;
	}

	public static boolean GenerateImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null || null == imgFilePath) // 图像数据为空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片
			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b, 0, b.length);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	@Value("${image.upload.temp.url}")
	private String imageUploadTempUrl;
	@RequestMapping(value = "uploadBase64Image")
	@ResponseBody
	public Map<Object, Object> uploadBase64Image(@RequestParam("uploadFile")  String uploadFile,@RequestParam(required = true) String fileName, FileType[] fileType,Boolean isFill,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(isFill == null)
			isFill = true;
		
		if(fileType == null) {
			fileType = new FileType[] { FileType.Temp };
		}
		uploadFile = uploadFile.split(",")[1];
		Map<Object, Object> result = new HashMap<Object, Object>();
		String ext = fileName.split("\\.")[1];
		String imageTmpUrl = imageUploadTempUrl + UUID.randomUUID().toString() + "" + "." + ext;
		GenerateImage(uploadFile, imageTmpUrl);
		BufferedImage sourceImg =ImageIO.read(new FileInputStream(imageTmpUrl));  
		if(sourceImg.getWidth()<200 || sourceImg.getHeight()<200) {
			result.put("fileUrl","");
			result.put("alert", "imageWidthTooSmall");
			return result;
		}
		
		File file = new File(imageTmpUrl);
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getName());
		String url = fileService.uploadImage(file, fileType, ext, isFill,request);
		result.put("alert", "");
		result.put("fileUrl", url);
		return result;
	}

}